package com.bluesmart.mia;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.util.Constant;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;
import com.util.Utils;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class ApplyActivity extends BaseActivity implements OnClickListener{
	int mSelected = -1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_apply);
		
		((TextView) findViewById(R.id.center)).setText(getString(R.string.apply_from_user));
		((Button) findViewById(R.id.right))
				.setCompoundDrawablesWithIntrinsicBounds(0, 0,
						R.drawable.a01, 0);
		
		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);
		findViewById(R.id.check0).setOnClickListener(this);
		findViewById(R.id.check1).setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		if(R.id.left == arg0.getId()) finish();
		else if(R.id.right == arg0.getId())
		{
			if(mSelected == 0)
			{
				startActivity(new Intent(this,AddDeviceActivity.class));
				finish();
			}
			else if(mSelected == 1)
			{
				sendHttpRequest();
			}
			else
			{
				ToastUtil.showToastWithAlertPic(R.string.check_err);
			}
		}
		else if(R.id.check1 == arg0.getId())
		{
			((TextView)findViewById(R.id.check0)).setCompoundDrawablesWithIntrinsicBounds(R.drawable.umeng_update_btn_check_off_holo_light, 0, 0, 0);
			if(mSelected == 1)
			{
				mSelected = -1;
				((TextView)findViewById(R.id.check1)).setCompoundDrawablesWithIntrinsicBounds(R.drawable.umeng_update_btn_check_off_holo_light, 0, 0, 0);
				findViewById(R.id.layout).setVisibility(View.INVISIBLE);
			}
			else
			{
				mSelected = 1;
				((TextView)findViewById(R.id.check1)).setCompoundDrawablesWithIntrinsicBounds(R.drawable.umeng_update_btn_check_on_holo_light, 0, 0, 0);
				findViewById(R.id.layout).setVisibility(View.VISIBLE);
			}
		}
		else if(R.id.check0 == arg0.getId())
		{
			findViewById(R.id.layout).setVisibility(View.INVISIBLE);
			((TextView)findViewById(R.id.check1)).setCompoundDrawablesWithIntrinsicBounds(R.drawable.umeng_update_btn_check_off_holo_light, 0, 0, 0);
			if(mSelected == 0)
			{
				mSelected = -1;
				((TextView)findViewById(R.id.check0)).setCompoundDrawablesWithIntrinsicBounds(R.drawable.umeng_update_btn_check_off_holo_light, 0, 0, 0);
			}
			else
			{
				mSelected = 0;
				((TextView)findViewById(R.id.check0)).setCompoundDrawablesWithIntrinsicBounds(R.drawable.umeng_update_btn_check_on_holo_light, 0, 0, 0);
			}
		}
	}
	
	private void sendHttpRequest() {
		final String email = ((EditText)findViewById(R.id.email)).getText().toString();
		if(email.length() == 0)
		{
			ToastUtil.showToastWithAlertPic(R.string.sign_in_tip);
			return;
		}
		
		if(!Utils.isEmail(email) && !Utils.isMobileNO(email))
		{
			ToastUtil.showToastWithAlertPic(R.string.sign_in_email);
			return;
		}
		
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);

				if (ruser.getString("resCode").equals("1000")) {
					findViewById(R.id.tip).setVisibility(View.VISIBLE);
					ToastUtil.showToastWithOkPic(R.string.apply_success);
					finish();
				}
				else if (ruser.getString("resCode").equals("2000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_2000);
				}
				else if (ruser.getString("resCode").equals("3000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_3000);
				}
				else 
				{
				}
			}
		};
		
		JSONObject params = new JSONObject();
		params.put("mia_un", PreferencesUtils.getString(this, "username"));
		params.put("pwd", PreferencesUtils.getString(this, "password"));
		params.put("applyUser", email);
		params.put("handType", "apply");

		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/ApplyToOtherUser.ashx", JSON.toJSONString(params), loginHandler);
	}

}
